/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.idea.common.error

import com.intellij.ide.util.treeView.NodeDescriptor
import com.intellij.ui.tree.BaseTreeModel
import java.util.concurrent.atomic.AtomicReference
import javax.swing.tree.TreePath

open class DesignerCommonIssueModel : BaseTreeModel<NodeDescriptor<*>>() {

  private val root = AtomicReference<DesignerCommonIssueNode>()

  override fun getRoot(): DesignerCommonIssueNode? {
    return root.get()
  }

  override fun getChildren(parent: Any): List<NodeDescriptor<*>> {
    val node = parent as? DesignerCommonIssueNode ?: return emptyList()
    val children = node.getChildren()
    if (children.isEmpty()) {
      return emptyList()
    }
    node.update()
    children.forEach { it.update() } // update presentation of child nodes before processing
    return children.toList()
  }

  fun setRoot(root: DesignerCommonIssueNode?) {
    this.root.set(root)
    structureChanged(null)
  }

  fun structureChanged(path: TreePath?) {
    treeStructureChanged(path, null, null)
  }

  override fun dispose() {
    super.dispose()
    setRoot(null)
  }
}
